package com.demo.swagger;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;

/**
 * Created by Samuel on 16/7/12.
 */
@ApiModel(value = "User得实体，----》",reference = "我是参考")
public class BaseRequestBean<T> implements Serializable {

    @ApiParam(value = "姓名--------------",required = true)
    private String code;            //加密签名key码
    private String sign;            //加密签名
    private Integer pageindex;      //分页index
    private Integer pagesize;       //分页size
    private String channel;         //渠道
    private String version;         //版本号


    @ApiParam(value = "自定义对象",required = true)
    private T resultObject;         //数据对象

    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getSign() {
        return sign;
    }
    public void setSign(String sign) {
        this.sign = sign;
    }
    public Integer getPageindex() {
        return pageindex;
    }
    public void setPageindex(Integer pageindex) {
        this.pageindex = pageindex;
    }
    public Integer getPagesize() {
        return pagesize;
    }
    public void setResultObject(T resultObject) {
        this.resultObject = resultObject;
    }
    public void setChannel(String channel) {
        this.channel = channel;
    }
    public String getVersion() {
        return version;
    }
    public void setVersion(String version) {
        this.version = version;
    }
    public void setPagesize(Integer pagesize) {
        this.pagesize = pagesize;
    }

    public T getResultObject() {
        return resultObject;
    }

    /**
     * 校验 反射对象是否成功
     * @param getClass
     * @return
     */
    public T getResultObject(Class getClass) {
        if (getClass==null){
            return resultObject;
        }
        if (resultObject!=null && resultObject.getClass().equals(getClass)){
            return resultObject;
        }else {
            return null;
        }
    }
    /**
     * 版本号记录
     * @return
     */
    public String getChannel() {
        if (channel !=null)
        {
            if (channel.equals("IOS"))
            {
                return "01";
            }
            else if (channel.equals("ANDROID")){
                return "02";
            }
            else if (channel.equals("WEB")){
                return "03";
            }
            else if (channel.equals("TEST")){
                return "04";
            }
            else {
                return "05";
            }
        }
        return null;
    }
    /**
     * 将 反射出来的数据 resultObject 处理成 tClass 类型对象
     * @param tClass
     * @return
     */
    public BaseRequestBean<T> resetResultObject(Class<T> tClass){
        return checks(tClass,false);
    }
    /**
     * 将 反射出来的数据 resultObject 处理成 List 的对象 将List里每一个对象处理成 tClass 类型对象
     * @param tClass
     * @return
     */
    public BaseRequestBean<T> resetArrayToResultObject(Class tClass)
     {
         return checks(tClass,true);
     }

    /**
     * 将反射对象处理成tClass或者Array<tClass>
     * @param tClass
     * @param isArray
     * @return
     */
    public BaseRequestBean checks(Class<T> tClass, boolean isArray){
        if (this.resultObject!=null && tClass!=null && this.resultObject.getClass().equals(String.class))
        {
            try {
                if(this.getPageindex()==null)this.setPageindex(0);
                if(this.getPagesize()==null || this.getPagesize()==0)this.setPagesize(10);
                if (StringUtils.isBlank((String) this.resultObject)){
                    this.resultObject=null;
                    return this;
                }
                Object object = null;
                if (isArray){
                    object = JSON.parseArray((String)this.resultObject,tClass);
                }
                else {
                    object = JSON.parseObject((String)this.resultObject,tClass);
                }
                this.resultObject = (T) object;

                return this;
            } catch (Exception e) {
                this.resultObject = null;
                e.printStackTrace();
            }

        }
        return this;
    }
}
